using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class InteractSign : MonoBehaviour
{
    private PlayerInput _input;
    private IInteractable _interact;
    private bool canPress;
    [SerializeField] private GameObject sign;
    private Transform playerTran => GameObject.FindGameObjectWithTag("Player").transform;

    private void Awake()
    {
        _input = new PlayerInput();

        _input.Gameplay.Interact.started += InteractThing;
    }
    
    
    private void Update()
    {
        sign.SetActive(canPress);
        sign.transform.localScale = playerTran.localScale;
    }

    private void InteractThing(InputAction.CallbackContext obj)
    {
        if(canPress)
            _interact.InteractAction();
    }

    private void OnEnable()
    {
        _input.Enable();
    }

    private void OnDisable()
    {
        _input.Disable();
    }

    private void OnTriggerStay2D(Collider2D other)
    {
        if (other.CompareTag("Can Interact"))
        {
            _interact = other.GetComponent<IInteractable>();
            canPress = true;
        }
    }

    private void OnTriggerExit2D(Collider2D other)
    {
        canPress = false;
    }
}
